﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;


namespace EnterpriseFund.Controllers
{
    public class QueryTaxAuditPolicyEvalController : Controller
    {
        //
        // GET: /QueryTaxAuditPolicyEval/
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        public ActionResult ReportViewerPartial()
        {
           // var strWhere = GetWhereString();
            var zsxx = GetModelVSBZSXX();
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryTaxAuditPolicyEval") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {

          
           for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {

                if (e.ColumnsInfo[i].FieldName == "NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth = 170;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth =320;

                }
                if (e.ColumnsInfo[i].FieldName == "FTaxClassAllowanceName")
                {
                    e.ColumnsInfo[i].ColumnWidth =80 ;
                }
                if (e.ColumnsInfo[i].FieldName == "FAllowanceName")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
                if (e.ColumnsInfo[i].FieldName == "FAllowanceTypeStr")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
               if (e.ColumnsInfo[i].FieldName == "FBaseAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth = 60;
                }
                if (e.ColumnsInfo[i].FieldName == "FOuterRate")
                {
                    e.ColumnsInfo[i].ColumnWidth =80 ;
                }
                if (e.ColumnsInfo[i].FieldName == "FInnerRate")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
               if (e.ColumnsInfo[i].FieldName == "FAllowanceAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth =120 ;
                }
            }
           
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "FAllowanceAmount")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;

            }

            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }
        public ActionResult Print()
        {
            GetWhereString();
            // CreatReport(table);
            return View("Print");
        }
        public string GetWhereString()
        {
            //绑定值

            var FTaxClassAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FTaxClassAllowanceName"), Request["FTaxClassAllowanceName"]);
            var nsrsbh = Request["nsrsbh"];
            var nsrbm = Request["nsrbm"];
            var nsrmc = Request["nsrmc"];
            var fyear = Request["FYear"];
            var fAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FAllowanceName"), Request["FAllowanceName"]);
            var sSRQBeginDate = Request["SSRQBeginDate"];
            var sSRQEndDate = Request["SSRQEndDate"];
            var iszf = Request["ISZF"];
            //ViewBag.FTaxClassAllowanceName = FTaxClassAllowanceName;
            //ViewBag.fyear = fyear;
            //ViewBag.nsrbm = nsrbm;
            //ViewBag.nsrmc = nsrmc;
            //ViewBag.FAllowanceName = fAllowanceName;
            //ViewBag.SSRQBeginDate = sSRQBeginDate;
            //ViewBag.SSRQEndDate = sSRQEndDate;

            ViewData["nsrsbh"] = nsrsbh;
            ViewData["FTaxClassAllowanceName"] = FTaxClassAllowanceName;
            ViewData["fyear"] = fyear;
            ViewData["nsrbm"] = nsrbm;
            ViewData["nsrmc"] = nsrmc;
            ViewData["FAllowanceName"] = fAllowanceName;
            ViewData["SSRQBeginDate"] = sSRQBeginDate;
            ViewData["SSRQEndDate"] = sSRQEndDate;


            var codenameBll = new BLL.AllowanceClass();
            ViewBag.FAllowanceNames = codenameBll.GetModelList("1=1");
            var filter = "1=1";
            if (!string.IsNullOrEmpty(nsrsbh))
                filter += string.Format(" and t1.nsrsbh like '%{0}%'", nsrsbh);
            if (!string.IsNullOrEmpty(nsrbm))
                filter += string.Format(" and t1.nsrbm like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(nsrmc))
                filter += string.Format(" and t1.nsrmc like '%{0}%'", nsrmc);
            if (!string.IsNullOrEmpty(iszf) && iszf == "true")
            {
                filter += string.Format(" and a.FStatus <> 9", iszf);
            }

            if (!string.IsNullOrEmpty(FTaxClassAllowanceName))
            {
                filter += string.Format(" and a.FTaxClassAllowanceNo = '{0}' ", FTaxClassAllowanceName);
            }
            if (!string.IsNullOrEmpty(fAllowanceName))
            {
                filter += string.Format(" and t3.FAllowanceClassNo = '{0}' ", fAllowanceName);
            }
            if (!string.IsNullOrEmpty(fyear))
            {
                filter += string.Format(" and a.FYear = '{0}' ", fyear);
            }

            if (!string.IsNullOrEmpty(sSRQBeginDate) || !string.IsNullOrEmpty(sSRQEndDate))
            {
                var begin = string.IsNullOrEmpty(sSRQBeginDate) ? "0" : sSRQBeginDate;
                var end = sSRQEndDate;
                filter += string.Format(" and FAllowanceAmount >= '{0}'", begin);
                if (!string.IsNullOrEmpty(sSRQEndDate))
                    filter += string.Format(" and FAllowanceAmount <= '{0}'", end);
            }

            return filter;

        }



        public List<Model.TaxAuditPolicyEval> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.TaxAuditPolicyEval().GetModelQueryList(strWhere);
            return zsxx;
        }
        [HttpPost]
        public ActionResult Index(string id)
        {
            var vsbzsxx = GetModelVSBZSXX();
         //   return View(vsbzsxx);
            return View();
        }

        [HttpGet]
        public ActionResult Index()
        {
      

            GetWhereString();
           // var vsbzsxx = GetModelVSBZSXX();
            return View();
        }

        public ActionResult QueryTaxAuditPolicyEvalListPartial()
        {
            GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            return View(vsbzsxx);
        }

        #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            var table = GetModelVSBZSXX();
            return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("政策鉴定查询导出.xls"), true);
        }

        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "FTaxAuditId";
            setting.CallbackRouteValues = new { Controller = "QueryTaxAuditPolicyEval", Action = "QueryTaxAuditPolicyEvalListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;

            setting.Columns.Add("NSRSBH", "纳税人识别号");
            setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRMC", "企业名称");
            setting.Columns.Add("FTaxClassAllowanceName", "征收项目");
            setting.Columns.Add("FAllowanceName", "补贴类型");
            setting.Columns.Add("FFinancePropName", "经济性质");
            setting.Columns.Add("FEnterprisePropName", "企业性质");
            setting.Columns.Add("FAllowanceTypeStr", "补贴方式");
            setting.Columns.Add("se", "税额");
            setting.Columns.Add("FBaseAmount", "基数");
            setting.Columns.Add("FOuterRate", "外补贴率");
            setting.Columns.Add("FInnerRate", "内补贴率");
            setting.Columns.Add("FAllowanceAmount", "补贴额");
            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "NSRSBH").DisplayFormat = "总计:";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAllowanceAmount").DisplayFormat = "￥{0}";
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAllowanceAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";

            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };
            setting.SettingsBehavior.EnableCustomizationWindow = true;
			
			
	
	
            setting.PreRender = setting.BeforeGetCallbackResult = (sender, e) =>
            {


                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QueryTaxAuditPolicyEval");


            };
            return setting;
        }
        public string GetList(string FYear)
        {
            string str="1=1";
            List<Model.TaxClassAllowance> TaxClassAllowanceList = new List<Model.TaxClassAllowance>();
            if(!string.IsNullOrEmpty(FYear))
            {
                str += string.Format(" and FYear={0}", FYear);
                BLL.TaxClassAllowance codeBLL = new BLL.TaxClassAllowance();
                TaxClassAllowanceList = codeBLL.GetModelList(str);
            }
           else
            {
                //str += string.Format(" and FYear={0}", FYear);
                BLL.TaxClassAllowance codeBLL = new BLL.TaxClassAllowance();
                TaxClassAllowanceList = codeBLL.GetModelList1(str);
            }
            return JsonConvert.SerializeObject(TaxClassAllowanceList);
        }
        #endregion

    }
}
